package com.qijian.maindata.mapper;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qijian.maindata.domain.BomDetail;
import com.qijian.maindata.domain.query.BomDetailInfoQuery;
import com.qijian.maindata.domain.query.BomDetailQuery;
import com.qijian.maindata.domain.vo.BomDetailAllVo;
import com.qijian.maindata.domain.vo.BomDetailInfoVo;
import com.qijian.maindata.domain.vo.BomDetailVo;
import com.qijian.maindata.domain.vo.SalesCapacityVo;
import com.qijian.tool.domain.Storage;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

/**
 * BOM明细Mapper接口
 *
 * @author qijian
 * @date 2021-09-01
 */
public interface BomDetailMapper extends BaseMapper<BomDetail> {
    List<BomDetailVo> selectVo(@Param(Constants.WRAPPER) Wrapper<BomDetailQuery> queryWrapper, @Param("type") Integer type);

    /**
     * 工艺收益数控件的钻取
     * @param query
     * @return
     */
    List<BomDetailVo> listNcDetail(@Param("query") BomDetailQuery query);

    /**
     * 工艺收益BOM的钻取
     * @param query
     * @return
     */
    List<BomDetailVo> listBomDetail(@Param("query") BomDetailQuery query);

    List<BomDetailVo> selectBomAndLineVo(@Param(Constants.WRAPPER) Wrapper<BomDetailQuery> queryWrapper);

    List<BomDetailAllVo> selectAllVo(@Param(Constants.WRAPPER) Wrapper<BomDetailQuery> queryWrapper);

    Page<BomDetailVo> selectPageVo(Page<BomDetail> page, @Param(Constants.WRAPPER) Wrapper<BomDetailQuery> queryWrapper);

    @Select("select b.detail_id,b.detail_code,b.line_code,b.item_id,i.item_code,b.total_quantity,i.item_name,i.drawing_no,i.part_no," +
            "i.item_weight,i.item_attr,p.process_id,p.process_code,p.process_name,pi.demand_id,pi.item_id as p_item_id, #{bomId} bomId ," +
            "pi.item_code AS p_item_code,pi.item_name AS p_item_name,pi.remark,s.url,b.sino_pushed,mi.item_classify as p_item_classify from main_bom_detail b " +
            "INNER join main_item i on b.item_id = i.item_id " +
            "INNER join main_line_process p on p.process_id = (select process_id from main_line_process where is_del = 0 and (process_type_name = '下料工序' or process_type = 'XL') and line_id = b.line_id ORDER BY process_id DESC limit 1) " +
//            "left join main_line_process_item pi on pi.demand_id = (select demand_id from main_line_process_item where is_del = 0 and process_id = p.process_id and line_id = p.line_id limit 1) " +
            "left join main_line_process_item pi on pi.demand_id = (select demand_id from main_line_process_item where is_del = 0 and bom_id = b.detail_id ORDER BY demand_id DESC limit 1) " +
            "left join tool_storage s on s.class_table = 'NC' and s.suffix = 'png' and CONCAT(i.drawing_no,'-',i.part_no,'_PNG') = s.name " +
            "left join main_item mi on mi.item_id = pi.item_id "+
            "where b.is_del = 0 and b.line_id is not null and b.bom_id = #{bomId} ")
    List<BomDetailVo> selectNc(Long bomId);


    List<BomDetailVo> selectNcPart(@Param("bomId") Long bomId,@Param("bomDetailIds") List<Long> bomDetailIds);

    Page<BomDetailInfoVo> selectBomInfo(Page<BomDetailInfoVo> page, @Param(Constants.WRAPPER) QueryWrapper<BomDetailInfoQuery> queryWrapper);

    /**
     * 查询所有工序附件
     *
     * @param bomId /
     * @return /
     */
    @Select("select d.detail_id class_id, s.name, s.url from main_bom_detail d " +
            "inner join main_line_process p on d.line_id = p.line_id " +
            "inner join tool_storage s on s.class_id = p.process_id " +
            "where d.is_del = 0 and p.is_del = 0 and s.is_del = 0 and d.bom_id = #{bomdId}")
    List<Storage> getFiles(Long bomId);


    List<SalesCapacityVo> selectSaleCapacity(Integer dataMonths);

    @Update("update main_bom_detail set is_del = 0 where detail_id = item_id")
    void recoveryBom(Long itemId);

    /**
     * 根据BomId查询历史线路信息
     *
     * @param bomId /
     * @return /
     */
    List<BomDetailVo> getHisLineByBomId(Long bomId);

    /**
     * 根据bomDetailId查询该bomDetail的所有工序
     *
     * @param bomDetailId bomDetailId
     * @return 所有工序
     */
    BomDetailVo selectBomDetailLineProcess(@Param("bomDetailId") Long bomDetailId);

    /**
     * 根据bomId，查出该bom下所有的工序类型名称
     * @param bomId
     * @return
     */
    BomDetailVo selectBomDetailLineProcessType(@Param("bomId") Long bomId);
}
